<template>
    <LDrawerForm
        :visible="visible"
        :confirmLoading="confirmLoading" 
        :title="title" 
        ref="sysparamForm" 
        :formAttributes="formAttributes" 
        @handleOk="handleOk" 
        @handleCancel="handleCancel"
        @changeEventHandler="changeEventHandler">
     
    </LDrawerForm>

</template>

<script>
import LDrawerForm from '@/components/lt/form/LDrawerForm'
import pick from 'lodash.pick'
import request from '@/utils/request'
import defaultSettings from '@/config/defaultSettings'


export default {
    name:'sysparamForm',
    components:{
        LDrawerForm
    },
    data(){
        return{
            title:'详情',
            defaultSettings,
            visible:false,
            confirmLoading: false,
            model:{},
            formAttributes:{
              type:'',//card,tabs
              formItems:[
                {
                  title:'标签一',
                  columns:1,
                  fields:[
                    {field:'paramCode',label:'参数编码',disabled:true,component:'input'},
                    {field:'paramType',label:'参数类型',disabled:false,initialValue:'1',component:'select',dictCode:defaultSettings.数据字典.系统参数类型,rules: [{required: true, message: '参数类型不能为空'}]},
                    {field:'paramVal',label:'参数值',component:'input',rules: [{required: true, message: '参数值不能为空'}]},
                    {field:'paramRemark',label:'参数说明',component:'textarea',rules: [{required: true, message: '参数说明不能为空'}]},
                  ]
                },
              ]

            }


        }
    },
    methods:{
       
        edit (record) {
            this.changeEventHandler({"paramType":"1"})
            this.visible = true;
            this.model = Object.assign({}, record);
            this.changeComponent(this.model.paramType);
            this.$nextTick(() => {
                this.$refs.sysparamForm.getForm().setFieldsValue(pick(this.model,'paramCode','paramVal','paramRemark','paramType'))
            })
        },
        handleOk(){
            const that = this;
            this.$refs.sysparamForm.getForm().validateFields((err, values) => {
                if (!err) {
                    that.confirmLoading = true
                    let formData = Object.assign(this.model, values);
                    request({url: '/WxSysConfigController/save',method: 'post',data: formData}).then((res)=>{
                        if(res.success){
                            that.close()
                            that.$message.success(res.message);
                            that.edit(res.result)
                        }else{
                            that.$message.warning(res.message);
                        }
                    }).finally(() => {
                        that.confirmLoading = false;
                        
                    })
                    
                }
            })
        },
        handleCancel(){
            this.visible=false
        },
        close () {
            this.$emit('close');
            this.visible = false;
        },
        changeComponent(paramType){
            let fieldVal = this.formAttributes.formItems[0].fields[2]
            if(paramType=="2"){
                   //数字
                  fieldVal.component="inputNumber" 
               }else if(paramType=="3"){
                   //年月日
                   fieldVal.component="date" 
               }else if(paramType=="4"){
                   //年月日时分秒
                   fieldVal.component="time" 
               }else if(paramType=="5"){
                   //时分秒
                   fieldVal.component="timePicker" 
                   fieldVal["valueFormat"]="HH:mm:ss"
               }else if(paramType=="1"){
                   //字符串
                   fieldVal.component="input"
               }else if(paramType=="6"){
                   //时分
                   fieldVal.component="timePicker" 
                   fieldVal["valueFormat"]="HH:mm"
               }else if(paramType=="7"){
                   //图片上传
                   fieldVal.component="imageUp" 
               }else if(paramType=="8"){
                   //文件上传
                   fieldVal.component="fileUp" 
               }
        },
        /**
         * 表单内容发生变化
         */
        changeEventHandler(changeItem){
           if(changeItem["paramType"]){
               let paramType = changeItem["paramType"]
               this.changeComponent(paramType)

           }
           
        }
    },
    created(){
        
    },
}
</script>